package com.ygqh.baby.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.http.ContentType;
import com.foxinmy.weixin4j.http.HttpHeaders;
import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.exception.YgHttpException;
import com.ygqh.baby.http.YgRequestExecutor;
import com.ygqh.baby.http.YgResponse;
import com.ygqh.baby.po.YgUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("/pushUserToOffShopHandler")
public class PushUserToOffShopHandler {
    private final YgRequestExecutor requestExecutor = new YgRequestExecutor();

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${off.push.user.url}")
    private String offPushUserUrl;

    public void setOffPushUserUrl(String offPushUserUrl) {
        this.offPushUserUrl = offPushUserUrl;
    }

    public void pushUserToOffShop(YgUser user, String offShop) {
        logger.info("pushUserToOffShop request: {},{}", JSON.toJSONString(user), offShop);
        if (StringUtil.isBlank(offShop)) {
            return;
        }
        new Thread(() -> {
            JSONObject object = new JSONObject();
            object.put("userName", user.getUserName());
            object.put("nickName", user.getNickName());
            object.put("headUrl", user.getHeadImageUrl());
            object.put("fromCode", user.getFromCode());
            object.put("appType", user.getAppType());
            object.put("platformNo", offShop);
            object.put("telPhone", user.getTelPhone());

            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("Content-Type", ContentType.APPLICATION_JSON.getMimeType());
            try {
                YgResponse post = requestExecutor.post(offPushUserUrl, httpHeaders, object.toJSONString());
                logger.info("pushUserToOffShop response ;{}", post.getAsString());
            } catch (YgHttpException e) {
                e.printStackTrace();
            }
        }).start();

    }
}
